package com.css.bjdt_core.summary.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.cell.entity.CellData;
import com.css.bjdt_core.cell.service.CellDataService;
import com.css.bjdt_core.cell.service.CellService;
import com.css.bjdt_core.util.ExecutorPool;

@Service
//报表数据汇总方法重写
public class ReportSummaryNewService {

	@Autowired
	private Dao dao;

	@Autowired
	private CellService cellService;

	@Autowired
	private CellDataService cellDataService;

	private static final Log logger = LogFactory.getLog(ReportSummaryNewService.class);

	public List<CellData> reportValueSummary(HttpSession session, List<Cell> cellList, List<String> dzzIdList,
			String annualId, String dzzId) {
		String tableName = "RPT_CELL_DATA_" + annualId;
		String sqlStr = "select CELL_ID, sum(CELL_VALUE) as CELL_VALUE from $tableName where DZZ_ID in $dzzIdList and CELL_TYPE in ('1','2','3') GROUP BY CELL_ID";

		StringBuilder dzzIdNewStr = new StringBuilder();
		for (String tmpId : dzzIdList) {
			dzzIdNewStr.append("'").append(tmpId).append("',");
		}
		String sqlNewStr = sqlStr.replace("$tableName", tableName).replace("$dzzIdList",
				"(" + dzzIdNewStr.substring(0, dzzIdNewStr.length() - 1) + ")");
		Sql sql = Sqls.create(sqlNewStr);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				Map<String, String> res = new HashMap<String, String>();
				while (rs.next()) {
					res.put(rs.getString("cell_id"), rs.getString("cell_value"));
				}
				return res;
			}
		});
		dao.execute(sql);
		Map<String, String> result = sql.getObject(HashMap.class);
		List<CellData> list = new ArrayList<CellData>();
		for(Cell cell : cellList) {
			String cellValue = result.containsKey(cell.getCellId()) ? result.get(cell.getCellId()) : "0";
			CellData cellData = new CellData(cell, cellValue, dzzId);
			cellData.setInsertDate(new Date());
			cellData.setSumStatus("1");
			list.add(cellData);
		}

		return list;
	}

	//保存汇总之后的数据
	public void saveReportValueSummary(List<CellData> cellDataList, String annualId) {
		cellDataService.batchInsertCellDataSummary(annualId, cellDataList);
	}

	public List<CellData> anyCellValueSummary(List<String> cellIdList, List<String> dzzIdList, String annualId,
			String dzzId) {
		List<CellData> cellDataList = new ArrayList<CellData>();
		String tableName = "RPT_CELL_DATA_" + annualId;
		String sqlStr = "select sum(CELL_VALUE) from $tableName where CELL_ID='$cellId' and DZZ_ID in $dzzIdList and CELL_TYPE in ('1','2','3')";

		String dzzIdNewStr = "";
		for (String dzzIdCond : dzzIdList) {
			dzzIdNewStr = dzzIdNewStr + "'" + dzzIdCond + "',";
		}
		for (String cellId : cellIdList) {
			String sqlNewStr = sqlStr.replace("$tableName", tableName).replace("$cellId", cellId).replace("$dzzIdList",
					"(" + dzzIdNewStr.substring(0, dzzIdNewStr.length() - 1) + ")");
			Sql sql = Sqls.create(sqlNewStr);
			sql.setCallback(Sqls.callback.str());
			dao.execute(sql);
			Cell cell = cellService.getACellEntity(cellId);
			String cellValue = sql.getObject(String.class);
			if (null == cellValue || cellValue.equals("")) {
				cellValue = "0";
			}
			CellData cellData = new CellData(cell, cellValue, dzzId);
			cellData.setInsertDate(new Date());
			cellData.setSumStatus("1");
			cellDataList.add(cellData);
		}
		return cellDataList;
	}
}
